package com.exsun.ex1077.ui.activity.vehiclelist

import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONObject
import com.drake.brv.PageRefreshLayout
import com.exsun.ex1077.network.apiService
import com.exsun.ex1077.network.entity.DeviceList
import com.zhangping.fastjetpack.base.BaseViewModel
import com.zhangping.fastjetpack.extend.base.ResultState
import com.zhangping.fastjetpack.extend.base.request
import com.zhangping.fastjetpack.extend.databind.IntObservableField
import com.zhangping.fastjetpack.model.KeyValue
import com.zhangping.fastjetpack.model.Tree

/**
 * 描述：
 * 作者：ZhangPing
 * 创建时间：2022/8/9 16:25
 */
class VehicleListViewModel: BaseViewModel() {

    val status = IntObservableField()
    val pageSize = 20
    var searchWord = ""
    var companyIds: List<String>? = null
    var objectTypes: List<String>? = null
    var startTime: String? = null
    var endTime: String? = null
    var totalCount = 0
    var onlineCount = 0
    var offlineCount = 0
    var notLocationCount = 0

    val listResult = MutableLiveData<ResultState<DeviceList>>()
    val deptTreeResult = MutableLiveData<ResultState<List<Tree>>>()
    val objectTypeResult = MutableLiveData<ResultState<List<KeyValue>>>()

    fun loadData(pageIndex: Int = PageRefreshLayout.startIndex){
        request({ apiService.vehicleList(JSONObject().apply {
            put("pageIndex", pageIndex)
            put("pageSize", pageSize)
            if (!companyIds.isNullOrEmpty()){
                put("companyIds", companyIds)
            }
            if (!objectTypes.isNullOrEmpty()){
                put("objTypes", objectTypes)
            }
            if (!startTime.isNullOrEmpty()){
                put("activateStartTime", startTime)
            }
            if (!endTime.isNullOrEmpty()){
                put("activateEndTime", endTime)
            }
            if (status.get() == VehicleListActivity.STATUS_ONLINE){
                //在线
                put("isOnline", 1)
            }else if (status.get() == VehicleListActivity.STATUS_OFFLINE){
                //离线
                put("isOnline", 0)
            }else if (status.get() == VehicleListActivity.STATUS_NOT_ONLINE){
                //未上线
                put("isOnline", 2)
            }
            if (searchWord.isNotEmpty()){
                put("text", searchWord)
            }
        })}, listResult)
    }

    fun loadDeptTree(){
        request({ apiService.vehicleDeptTree()}, deptTreeResult)
    }

    fun loadManageTypeList(){
        request({ apiService.manageTypeList()}, objectTypeResult)
    }

    fun deviceDelete(id: String): MutableLiveData<Boolean>{
        val liveData = MutableLiveData<Boolean>()
        request({ apiService.deviceDelete(id)}, {
            liveData.value = true
        })
        return liveData
    }
}